package boun.center;
import java.util.ArrayList;
import java.util.HashMap;

public class Updater {
    private ArrayList<CmpeNews> cmpeNews = null;
    private ArrayList<BounNews> bounNews = null;
    private ArrayList<CalendarEvent> calendar = null;
    private ArrayList<Tweet> tweets = null;
    private HashMap<String,String> busScheduleUrlMap;
    private HashMap<String, BusSchedule> busScheduleMap;
    private ArrayList<Thread> threads;
    
    public Updater() {
        threads = new ArrayList<Thread>();
        busScheduleUrlMap = new HashMap<String, String>();
        busScheduleMap = new HashMap<String, BusSchedule>();
        busScheduleUrlMap.put("59R", "http://www.iett.gov.tr/saat/orer.php?hid=hat&hatcode=59R%3ARUMEL%DD+H%DDSAR%DCST%DC+-+%DE%DD%DEL%DD&BtnSaatler2.x=29&BtnSaatler2.y=7");
        busScheduleUrlMap.put("559C", "http://www.iett.gov.tr/saat/orer.php?hid=hat&hatcode=559C%3ARUMEL%DDH%DDSAR%DCST%DC-TAKS%DDM&BtnSaatler2.x=13&BtnSaatler2.y=7");
        busScheduleUrlMap.put("43R", "http://www.iett.gov.tr/saat/orer.php?hid=hat&hatcode=43R%3ARUMEL%DD+H%DDSAR%DCST%DC+-+KABATA%DE&BtnSaatler2.x=10&BtnSaatler2.y=11");
        busScheduleUrlMap.put("59RS", "http://www.iett.gov.tr/saat/orer.php?hid=hat&hatcode=59RS%3ASARIYER-RUMEL%DDH%DDSAR%DCST%DC&BtnSaatler2.x=22&BtnSaatler2.y=7"); 
    }
    
    private void updateCmpeNews() {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //System.out.println("cmple news basla");
                    cmpeNews = CmpeNewsFetcher.fetchCmpeNews();
                    //System.out.println("cmpe news bitir");
                } catch (Exception e) {
                    cmpeNews = null;
                    e.printStackTrace();
                }
            }
        });
        threads.add(t);
        t.start();
    }
    
    private void updateBounNews() {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //System.out.println("Boun news basla");
                    bounNews = BounNewsFetcher.fetchBounNews();
                    //System.out.println("Boun news bitir");
                } catch (Exception e) {
                    bounNews = null;
                    e.printStackTrace();
                }
            }
        });
        threads.add(t);
        t.start();
    }

    private void updateCalendar() {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //System.out.println("calendar basla");
                    CalendarFetcher fetcher = new CalendarFetcher();
                    calendar = fetcher.fetchCalendar();
                    //System.out.println("calendar bitir");
                } catch (Exception e) {
                    bounNews = null;
                    e.printStackTrace();
                }
            }
        });
        threads.add(t);
        t.start();
    }
 
    private void updateTwitter() {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //System.out.println("twitter basla");
                    tweets = TwitterFetcher.fetchTweets();
                    //System.out.println("twitter bitir");
                } catch (Exception e) {
                    bounNews = null;
                    e.printStackTrace();
                }
            }
        });
        threads.add(t);
        t.start();
    }

    private void updateBusSchedule() {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    busScheduleMap.clear();
                    //System.out.println("bus schedule basla");
                    for (String routeName : busScheduleUrlMap.keySet()) {
                        String url = busScheduleUrlMap.get(routeName);
                        busScheduleMap.put(routeName, BusScheduleFetcher.fetchBusSchedule(url));
                    }
                    //System.out.println("bus schedule bitir");
                } catch (Exception e) {
                    bounNews = null;
                    e.printStackTrace();
                }
            }
        });
        threads.add(t);
        t.start();
    }
    
    public synchronized boolean updateAll() {
        threads.clear();
        updateCmpeNews();
        updateBounNews();
        updateCalendar();
        updateTwitter();
        updateBusSchedule();
        for (Thread t : threads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("Updater threads have joined!");
        return true;
    }

    public ArrayList<BounNews> getBounNews() {
        return bounNews;
    }

    public ArrayList<CmpeNews> getCmpeNews() {
        return cmpeNews;
    }

    public ArrayList<CalendarEvent> getCalendar() {
        return calendar;
    }

    public ArrayList<Tweet> getTweets() {
        return tweets;
    }
    
   public ArrayList<String> getBusSchedule(String routeName, BusSchedule.DayType dayType, BusSchedule.Direction direction) {
       BusSchedule bs = busScheduleMap.get(routeName);
       return bs == null ? null : bs.getDailyTimes(direction, dayType);
   }
}
